package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.PackageDto;
import com.huaxiafinance.management.mapper.PackageOrderMapper;
import com.huaxiafinance.management.mapper.PartnerInfoMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.model.PackageOrder;
import com.huaxiafinance.management.model.PartnerInfo;
import com.huaxiafinance.management.service.PartnersDetailsService;
import com.huaxiafinance.management.util.DateUtil;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huangwencheng on 2017/12/07.
 */
@Service("PartnersDetailsService")
@Transactional
@SuppressWarnings("SpringJavaAutowiringInspection")
public class PartnersDetailsServiceImpl implements PartnersDetailsService {

    private static final Logger log = Logger.getLogger(PartnersDetailsServiceImpl.class);

    @Autowired
    private PartnerInfoMapper partnerInfoMapper;

    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    @Autowired
    private PackageOrderMapper packageOrderMapper;

    /**
     * 获取套餐状态列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public MessageResult queryPackageType() throws Exception {
        MessageResult messageResult = new MessageResult();
        List<Map<String, Object>> map = yfqDictionaryMapper.selectDictList(CommonConstant.PACKAGE_TYPE);
        messageResult.setData(map);
        messageResult.setSuccessResult();
        return messageResult;
    }

    /**
     * 获取用户个人信息
     *
     * @param userUuid 用户userUuid
     * @return
     * @throws Exception
     */
    @Override
    public MessageResult queryPartner(String userUuid) throws Exception {
        MessageResult messageResult = new MessageResult();
        if (userUuid==null){
            messageResult.setFailureResult("userUuid不能为空");
            return messageResult;
        }
        PartnerInfo partnerInfo = partnerInfoMapper.queryByUserUuid(userUuid);
        messageResult.setSuccessResult();
        messageResult.setData(partnerInfo);
        return messageResult;
    }

    /**
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    public MessageResult queryMembershipMeal(PackageDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        //计算查询时间
        if (dto.getStartDate()!=null&&dto.getEndDate()!=null
                &&!("").equals(dto.getStartDate())
                &&!("").equals(dto.getEndDate())){
            Map<String, Date> dateMap = DateUtil.getTimeFormat(dto.getStartDate(), dto.getEndDate());
            dto.setStartTime(dateMap.get("beginTime"));
            dto.setEndTime(dateMap.get("overTime"));
        }else {
            dto.setStartTime(null);
            dto.setEndTime(null);
        }
        //判断选择订单状态
        dto.setType(dto.getType()-2);
        dto.setPage(dto.getPage() * dto.getRows());
        List<PackageOrder> packageOrderList = packageOrderMapper.queryByDto(dto);
        int count = packageOrderMapper.countByDto(dto);
        messageResult.setData(packageOrderList);
        messageResult.setRows(count);
        messageResult.setSuccessResult();
        return messageResult;
    }


}
